/*
 * Created on 30-ene-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.juanfrivaldes.cio2005.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;

import com.juanfrivaldes.cio2005.domain.Usuario;

/**
 * @author root
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class AltaPonenciaForm extends ActionForm {

	private static Log log = LogFactory.getLog(AltaPonenciaForm.class);

	private String coautor;

	/**
	 * @return Returns the coautor.
	 */
	public String getCoautor() {
		return coautor;
	}
	
	/**
	 * @param coautor The coautor to set.
	 */
	public void setCoautor(String coautor) {
		this.coautor = coautor;
	}
	private FormFile resumen;

	/**
	 * @return Returns the categoria.
	 */
	public String getCategoria() {
		return categoria;
	}

	/**
	 * @param categoria
	 *            The categoria to set.
	 */
	public void setCategoria(String categoria) {
		this.categoria = categoria;
	}

	/**
	 * @return Returns the titulo.
	 */
	public String getTitulo() {
		return titulo;
	}

	/**
	 * @param titulo
	 *            The titulo to set.
	 */
	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}

	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 3256723961709410102L;

	private String titulo;

	private String categoria;

	public void reset(ActionMapping mapping, HttpServletRequest request) {

		titulo = null;
		categoria = null;
		coautor=null;
		resumen=null;

	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
			
		
		ActionErrors errors = new ActionErrors();
		
		// validamos el coautor solo si se trata de agregar
		assert((String)request.getParameter("action")!=null);
		if(((String)request.getParameter("action")).equals("Añadir coautor")){
			if(coautor==null || coautor.trim().equals("")){
				log.trace("Coautor nulo");
				errors.add("coautor",new ActionMessage("altaPonencia.coautorNulo.problema"));
			}
			
			return errors;
		}
		
		if (titulo == null || titulo.trim().equals("")) {
			errors.add("titulo", new ActionMessage(
					"altaPonencia.titulo.problema"));
			return errors;

		}

		if (categoria == null || categoria.trim().equals("")) {
			errors.add("categoria", new ActionMessage(
					"altaPonencia.categoria.problema"));
			return errors;
		}

		
		if(resumen==null|| resumen.getFileName().equals("")||resumen.getFileSize()==0){
			errors.add("resumen", new ActionMessage(
			"altaPonencia.file.problema"));
			return errors;
		}
		
		String name=resumen.getFileName();
		log.trace("Fichero "+name.substring(name.lastIndexOf('.')).toUpperCase());
		
			
			if(!name.substring(name.lastIndexOf('.')).toUpperCase().equals(".PDF")){
				errors.add("resumen",new ActionMessage("altaPonencia.noPdf.problema"));
				return errors;
			}
		

		if(this.resumen!=null){
				
			//comprobamos el fichero
				 Boolean maxLengthExceeded =
		            (Boolean) request.getAttribute(
		                MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
		                
		        if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())) {
		        	log.trace("se excedio el tamaño maximo de fichero");
		        	errors.add("resumen",new ActionMessage("altaPonencia.tamanio.problema"));
		            return errors;
		        }
		}

		return errors;
	}

	/**
	 * @return Returns the resumen.
	 */
	public FormFile getResumen() {
		return resumen;
	}
	/**
	 * @param resumen The resumen to set.
	 */
	public void setResumen(FormFile resumen) {
		this.resumen = resumen;
	}
}
